package com.dzt.androidframework.database;

import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.View;

import com.dzt.androidframework.R;
import com.dzt.androidframework.activity.ActivityTitle;
import com.dzt.androidframework.database.tasks.TasksFilterType;
import com.dzt.androidframework.database.tasks.TasksFragment;
import com.dzt.androidframework.database.tasks.TasksLocalDataSource;
import com.dzt.androidframework.database.tasks.TasksPresenter;

import org.xutils.view.annotation.Event;

/**
 * 数据库操作的MVP架构，任务管理程序
 * 2017.06.19
 */
public class DatabaseActivity extends ActivityTitle{
	private static final String CURRENT_FILTERING_KEY = "CURRENT_FILTERING_KEY";
	private TasksPresenter tasksPresenter;
	private TasksFragment tasksFragment;

	@Override
	protected int getLayoutId() {
		return R.layout.activity_database;
	}

	@Override
	protected void initParams() {
		setTitleView(R.string.menu_2);
		FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
		tasksFragment = new TasksFragment();
		ft.add(R.id.contentFrame, tasksFragment);
		ft.commitAllowingStateLoss();

		TasksLocalDataSource localDataSource = TasksLocalDataSource.getInstance();
		tasksPresenter = new TasksPresenter(localDataSource, tasksFragment);
		tasksFragment.setPresenter(tasksPresenter);
		if (savedInstanceState != null) {
			TasksFilterType currentFiltering =
					(TasksFilterType) savedInstanceState.getSerializable(CURRENT_FILTERING_KEY);
			tasksPresenter.setFiltering(currentFiltering);
		}
	}

	@Override
	protected void initData(Bundle savedInstanceState) {

	}

	@Override
	protected String[] initPermissions() {
		return null;
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		outState.putSerializable(CURRENT_FILTERING_KEY, tasksPresenter.getFiltering());
		super.onSaveInstanceState(outState);
	}

	@Event(value = {R.id.fab})
	private void onClickView(View view) {
		switch (view.getId()) {
			case R.id.fab:
				tasksPresenter.addNewTask();
				break;
			default:
				break;
		}
	}
}
